// pages/advance/advance.js
var app = getApp()
var that
Page({
  /** 
   * 页面的初始数据   
   */
  data: {
    text: "This is page data.", 
    currentTab: 0,
    isStatus: 'pay',
    orders: {},
    ordersn:null,
    half_pri:0,    //房间半价
    is_holiday:false,  //该订单是否在假期内
    holiday_name:"",
    tixin:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this
    that.setData({ ordersn: options.ordersn})
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            winHeight: res.windowHeight
          });
        }
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  onShow: function () {
    app.req("get_orderroom.html", { "openid": app.data.openid, "ordersn": that.data.ordersn }, function (res) {
      console.log(res)
      that.holiday();  //判断当前订单是否在节假日期间
      if (res.status == "success") {
        res.mess.createtime1 = res.mess.createtime.substr(0, 10)
        res.mess.createtime2 = res.mess.createtime.substr(11, 5)
        if (res.mess.paytime != 0) {
          res.mess.createtime3 = res.mess.paytime.substr(0, 10)
          res.mess.createtime4 = res.mess.paytime.substr(11, 5)
        }
        if (res.mess.fuwutime != 0) {
          res.mess.createtime5 = res.mess.fuwutime.substr(0, 10)
          res.mess.createtime6 = res.mess.fuwutime.substr(11, 5)
        }
        if (res.mess.endtime != 0) {
          res.mess.createtime7 = res.mess.endtime.substr(0, 10) 
          res.mess.createtime8 = res.mess.endtime.substr(11, 5)
        }
        var p = parseFloat(res.mess.price).toFixed(2);
        var pri = parseFloat(p)/(((res.mess.catnum - res.mess.roomnum) + (res.mess.roomnum * 2)).toFixed(2) * res.mess.day)
        console.log(parseFloat(pri));
        if (res.mess.catnum < res.mess.roomnum){//猫数少于房数，半价计算方式改变
          pri = parseFloat(p)/(res.mess.roomnum*res.mess.day*2);
        }
        console.log(parseFloat(pri));
        that.setData({ orders: res, half_pri: parseFloat(pri) });
        that.get_cat();
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.onShow()
          }
        })
      }
    })
  
  },
  //改变选项卡
  bindChange: function (e) {
    var that = this;
    //that.setData({ currentTab: e.detail.current });
  },
  cellphone:function(){
    wx.makePhoneCall({
      phoneNumber: app.hotelData.house_notict.phone //真实的电话号码
    })
  },
  to_pay: function () {  //未付款时可点击  去支付
    app.req("repay_order.html", { "openid": app.data.openid, "ordersn": that.data.ordersn,'shop_id':wx.getStorageSync('shop_id')}, function (res) {
      if (res.status == "success") {
        wx.requestPayment(
          {
            'timeStamp': res.mess.timeStamp,
            'nonceStr': res.mess.nonceStr,
            'package': res.mess.package,
            'signType': res.mess.signType,
            'paySign': res.mess.paySign,
            'success': function (re) {
              app.toast("支付成功！")//设置状态
              that.data.orders.mess.status=1
              that.setData({ orders: that.data.orders})
            },
            'fail': function (re) {
              app.modal("提示", "支付失败，半小时内未支付房费该订单将会被删除！", function (re) { })
              // app.toast("支付失败！")
            },
            'complete': function (res) { }
          })

      } else if (res.status == "error") {
        app.toast(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.to_pay()
          }
        })
      }
    })
  },
  giveup_adv:function(){  //点击取消预约
    // var d = parseInt(Data.calculateTime(this.data.end_d, that.data.orders.mess.createtime));
    
    var time_start = Date.parse(that.data.orders.mess.start_time);   
    var time_now = Date.parse(new Date());
    var time_h = (time_now - time_start)/(3600);
    var del_price =0; //设置扣除的钱数
    if (that.data.is_holiday==true){ //若在法定假日期间
      if (time_h < 7*24) {
        var d = ((time_now + (7 * 24 * 3600)) - time_start) / (24*3600);
        if(d<that.data.orders.mess.day){
          del_price =(that.data.orders.mess.price / that.data.orders.mess.day)*d
        } else { del_price = that.data.orders.mess.price; }
      } 
    }else{
      if (time_h < 24 && time_h > 0) {
        del_price = that.data.orders.mess.price / that.data.orders.mess.day;
      }
      if (time_h > 0){//住房日期到了还没来住，要取消预约
        var d = ((time_now + (7 * 24 * 3600)) - time_start) / (24 * 3600);
        if (d < that.data.orders.mess.day) {
          del_price = (that.data.orders.mess.price / that.data.orders.mess.day) * d
        } else { del_price = that.data.orders.mess.price; }
      }
    }
    var p = "扣除"+del_price+"元，"
    if (del_price==0){p=''}
    
    app.modal("提示", that.data.holiday_name+"取消申请提交后，"+p+"在24小时内返回余额，是否提交申请？", function (re) {
      if (re) {
        app.req("refund.html", { "openid": app.data.openid, "ordersn": that.data.ordersn, "return_money": that.data.orders.mess.price - del_price,"type":"house" }, function (res) {
          if (res.status == "success") {
            app.modal("提示", res.mess)
          } else if (res.status == "error") {
            app.toast(res.mess)
          } else {
            app.modal("错误提示", "网络错误，是否重试？", function (re) {
              if (re) {
                that.giveup_adv()
              }
            })
          }
        })
      }
    })
  },
  early_go: function () {  //点击提前退房
    // app.req("refund.html", { "openid": app.data.openid, "ordersn": that.data.ordersn,"type":"house" }, function (res) {
    //   if (res.status == "success") {
    //     app.toast(res.mess)
    //   } else if (res.status == "error") {
    //     app.toast(res.mess)
    //   } else {
    //     app.modal("错误提示", "网络错误，是否重试？", function (re) {
    //       if (re) {
    //         that.onLoad()
    //       }
    //     })
    //   }
    // })
  },
  holiday: function (){
   
    app.req("holiday_date.html", {}, function (res) {
      if (res.status == "success") {
        for (var i = 0; i < res.mess.length;i++){
          var h1 = res.mess[i].s_time
          var h2 = res.mess[i].e_time
          var t1 = Date.parse(that.data.orders.mess.start_time);
          var t2 = Date.parse(that.data.orders.mess.end_time);
          // var t1 = 1506787290;
          // var t2 = 1596787200;
          if ((t1 >= h1 && t2 <= h2) || (t1 <= h1 && t2 >= h2) || (t1 > h1 && t1 < h2) || (t2 < h2 && t2 < h2)) {
            that.setData({ is_holiday: true, holiday_name: res.mess[i].name+"期间，" });
            
          }
        }
      } else if (res.status == "error") {
        app.toast(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.holiday()
          }
        })
      }
    })
 },
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current});
  },
  //选项卡标题绑定的事件
  swichNav: function (e) {
    var that = this;
    /*if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      var current = e.target.dataset.current;
      that.setData({
        currentTab: parseInt(current),
        isStatus: e.target.dataset.otype,
      });
      //没有数据就进行加载
      switch (that.data.currentTab) {
        case 0:
          !that.data.orderList0.length && that.loadOrderList();
          break;
        case 1:
          !that.data.orderList1.length && that.loadOrderList();
          break;
      }
    };*/
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      var current = e.target.dataset.current;
      that.setData({
        currentTab: parseInt(current),
      });
    };
  },
  get_cat: function () {  //获取个人的猫
    app.req("get_mycatnum.html", { "openid": app.data.openid }, function (res) {
      if (res.status == "success") {     //获取猫的信息
        console.log(res);
        if (res.mess.length <parseInt(that.data.orders.mess.catnum)){
          that.setData({ tixin:true });
        }
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_cat()
          }
        })
      }
    })
  }
})